/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Database;

import Domain.Activity;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

/**
 *
 * @author Min0
 */
public class DBActivity implements IFDBActivity
{
    private Connection con;

    public DBActivity()
    {
        con = DBConnection.getInstance().getDBcon();
    }

    //START private methods for class
    private Activity BuildActivity(ResultSet results)
    {
        Activity appObj = new Activity();
        try
        {
            appObj.setActivityID(results.getString("ActivityID"));
            appObj.setActivity(results.getString("Activity"));
            appObj.setSportFacilityNo(results.getInt("SportFacilityNo"));
            appObj.setDate(results.getString("Date"));
            appObj.setTime(results.getString("Time"));
            
        } catch (Exception e)
        {
            System.out.println("error in building the Activity object\n" + e.getMessage());
        }
        return appObj;
    }

    private String BuildQuery(String wClause)
    {
        String query = "SELECT * FROM Activity";

        if (wClause.length() > 0)
        {
            query = query + " WHERE " + wClause;
        }

        return query;
    }

    private Activity singleWhere(String wClause)
    {
        ResultSet results;
        Activity appObj = new Activity();
        String query = BuildQuery(wClause);
        System.out.println(query);
        try
        {
            java.sql.Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            results = stmt.executeQuery(query);

            if (results.next())
            {
                appObj = BuildActivity(results);
                //assocaition is to be build
                stmt.close();
            }

        }//end try	
        catch (Exception e)
        {
            System.out.println("Query exception: " + e);
        }
        return appObj;
    }

    private ArrayList<Activity> miscWhere(String wClause)
    {
        ResultSet results;
        ArrayList<Activity> list = new ArrayList<Activity>();

        String query = BuildQuery(wClause);

        try
        { // read the Activity from the database
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            results = stmt.executeQuery(query);

            while (results.next())
            {
                Activity appObj = new Activity();
                appObj = BuildActivity(results);
                list.add(appObj);
            }//end while
            stmt.close();
        } catch (Exception e)
        {
            System.out.println("Query exception - select: " + e);
        }
        return list;
    }
    //END private methods for class

    @Override
    public void insertActivity(Activity activity)
    {
        String query = "INSERT INTO Activity(ActivityID,Activity,SportFacilityNo,Date,Time) VALUES('"
                +activity.getActivityID()+"','"
                +activity.getActivity()+"',"
                +activity.getSportFacilityNo()+",'"
                +activity.getDate()+"','"
                +activity.getTime()+"')";
        
        System.out.println("insert: " + query);
        try
        {
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            stmt.executeUpdate(query);
            stmt.close();
        }//end try	
        catch (Exception ex)
        {
            System.out.println("Insert exception in activity db: " + ex);
        }
    }

    @Override
    public void insertActivity(String ActivityID, String Activity, int SportFacilityNo, String Date, String Time)
    {
        String query = "INSERT INTO Activity(ActivityID,Activity,SportFacilityNo,Date,Time) VALUES('"
                +ActivityID+"','"
                +Activity+"',"
                +SportFacilityNo+",'"
                +Date+"','"
                +Time+"')";
        
        System.out.println("insert: " + query);
        try
        {
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            stmt.executeUpdate(query);
            stmt.close();
        }//end try	
        catch (Exception ex)
        {
            System.out.println("Insert exception in activity db: " + ex);
        }
    }

    @Override
    public Activity getActivity(String ActivityID)
    {
        String wClause = " ActivityID ='"+ActivityID+"'";
        return singleWhere(wClause);
    }

    @Override
    public ArrayList<Activity> getActivityList()
    {
        return miscWhere("");
    }

    @Override
    public void updateActivity(String ActivityID, String Activity, int SportFacilityNo, String Date, String Time)
    {
        String query = "UPDATE Apartment SET "
                + "Activity ='" + Activity + "', "
                + "SportFacilityNo =" + SportFacilityNo + ", "
                + "Date = '" + Date + "',"
                + "Time = '" + Time + "'"
                + " WHERE ActivityID = '" + ActivityID + "'";
        System.out.println("Update query:" + query);
        try
        {
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            stmt.executeUpdate(query);
            stmt.close();
        } catch (Exception ex)
        {
            System.out.println("Update exception in activity db: " + ex);
        }
    }

    @Override
    public void updateActivity(String ActivityID, String Activity)
    {
        String query = "UPDATE Apartment SET "
                + "Activity ='" + Activity + "', "
                + " WHERE ActivityID = '" + ActivityID + "'";
        System.out.println("Update query:" + query);
        try
        {
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            stmt.executeUpdate(query);
            stmt.close();
        } catch (Exception ex)
        {
            System.out.println("Update exception in activity db: " + ex);
        }
    }

    @Override
    public void updateActivity(String ActivityID, int SportFacilityNo)
    {
        String query = "UPDATE Apartment SET "
                + "SportFacilityNo =" + SportFacilityNo + ", "
                + " WHERE ActivityID = '" + ActivityID + "'";
        System.out.println("Update query:" + query);
        try
        {
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            stmt.executeUpdate(query);
            stmt.close();
        } catch (Exception ex)
        {
            System.out.println("Update exception in activity db: " + ex);
        }
    }

    @Override
    public void updateActivityDate(String ActivityID, String Date)
    {
        String query = "UPDATE Apartment SET "
                + "Date = '" + Date + "',"
                + " WHERE ActivityID = '" + ActivityID + "'";
        System.out.println("Update query:" + query);
        try
        {
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            stmt.executeUpdate(query);
            stmt.close();
        } catch (Exception ex)
        {
            System.out.println("Update exception in activity db: " + ex);
        }
    }

    @Override
    public void updateActivityTime(String ActivityID, String Time)
    {
        String query = "UPDATE Apartment SET "
                + "Time = '" + Time + "'"
                + " WHERE ActivityID = '" + ActivityID + "'";
        System.out.println("Update query:" + query);
        try
        {
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            stmt.executeUpdate(query);
            stmt.close();
        } catch (Exception ex)
        {
            System.out.println("Update exception in activity db: " + ex);
        }
    }

    @Override
    public void deleteActivity(String ActivityID)
    {
        String query = "DELETE FROM Activity WHERE ActivityID = '"
                + ActivityID + "'";
        System.out.println(query);
        try
        { // delete from apartment
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            stmt.executeUpdate(query);
            stmt.close();
        } catch (Exception ex)
        {
            System.out.println("Delete exception in activity db: " + ex);
        }
    }
    
    @Override
    public int getMax()
    {
        int max = getMax.getMaxId("Select max(ActivityID) from Activity");
        return max;
    }
}
